package manager

import (
	"BlazeRider-backend/app/db"
	"database/sql"
	"fmt"
	"sync"
)

// ClientErrorRecordMysqlStorage 管理礼物赠送记录
type ClientErrorRecordMysqlInfo struct {
	LiveRoomId string       `db:"live_room_id"`
	Desc       string       `db:"desc"`
	Env        string       `db:"env"`
	ExtraS1    string       `db:"extra_s_1"`
	ExtraS2    string       `db:"extra_s_2"`
	ExtraS3    string       `db:"extra_s_3"`
	ExtraS4    string       `db:"extra_s_4"`
	ExtraS5    string       `db:"extra_s_5"`
	ExtraI1    string       `db:"extra_i_1"`
	ExtraI2    string       `db:"extra_i_2"`
	ExtraI3    string       `db:"extra_i_3"`
	ExtraI4    string       `db:"extra_i_4"`
	ExtraI5    string       `db:"extra_i_5"`
	CreateAt   sql.NullTime `db:"create_at"` // 使用 NullTime
	UpdateAt   sql.NullTime `db:"update_at"` // 使用 NullTime
}

type ClientErrorRecordMysqlStorage struct {
	roomGiftRecords map[string]map[string][]GiftRecord
	db              *sql.DB
	mu              sync.Mutex
}

func NewClientErrorRecordMysqlStorage() (*ClientErrorRecordMysqlStorage, error) {
	return &ClientErrorRecordMysqlStorage{
		roomGiftRecords: make(map[string]map[string][]GiftRecord),
	}, nil
}

// AddGiftRecord 增加礼物赠送记录
func (grm *ClientErrorRecordMysqlStorage) AddClientErrorRecord(liveRoomID, desc, env, extraS1, extraS2, extraS3, extraS4, extraS5 string, extraI1, extraI2, extraI3, extraI4, extraI5 int) error {
	_, err := db.GetDB().Exec(`
		INSERT INTO client_err_record 
			(live_room_id, description, env, extra_s_1, extra_s_2, extra_s_3, extra_s_4, extra_s_5, extra_i_1, extra_i_2, extra_i_3, extra_i_4, extra_i_5, create_at, update_at)
		VALUES 
			(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
	`,
		liveRoomID, desc, env, extraS1, extraS2, extraS3, extraS4, extraS5, extraI1, extraI2, extraI3, extraI4, extraI5) // 更新值参数

	if err != nil {
		return fmt.Errorf("create client error record error : %v", err)
	}
	return nil
}
